package org.binglan.youziyuepai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.binglan.youziyuepai.entity.ActivityToUser;
import org.binglan.youziyuepai.exception.MyException;
import org.binglan.youziyuepai.mapper.ActivityToUserMapper;
import org.binglan.youziyuepai.service.ActivityToUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 活动与用户多对多表 服务实现类
 * </p>
 *
 * @author sanjin
 * @since 2019-04-11
 */
@Service
public class ActivityToUserServiceImpl extends ServiceImpl<ActivityToUserMapper, ActivityToUser> implements ActivityToUserService {

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ActivityToUser> queryActivityToUsersByActivityId(Integer activiTyId) {
        ActivityToUser activityToUser = new ActivityToUser();
        activityToUser.setActivityId(activiTyId);
        activityToUser.setStatus(null);

        return list(new QueryWrapper<>(activityToUser));
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean isExistsActivityToUserByUserIdAndActivityId(String userId, Integer activityId) {
        ActivityToUser activityToUser = new ActivityToUser();
        activityToUser.setActivityId(activityId);
        activityToUser.setUserId(userId);

        return this.getOne(new QueryWrapper<>(activityToUser)) != null;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean saveActivityToUser(ActivityToUser activityToUser) {
        return this.save(activityToUser);
    }

    @Override
    public ActivityToUser queryActivityToUsersByActivityId(String userId, Integer activityId) {
        ActivityToUser activityToUser = new ActivityToUser();
        activityToUser.setActivityId(activityId);
        activityToUser.setUserId(userId);
        return this.getOne(new QueryWrapper<>(activityToUser));
    }

    @Override
    public boolean deleteActivityToUserById(Integer id) {

        return this.removeById(id);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public ActivityToUser changeActivityToUserStatus(String userId, Integer applyId, Integer code) {
        ActivityToUser apply = this.getById(applyId);
        if (apply == null) {
            throw new MyException("applyId 不正确");
        }
        if (apply.getStatus().equals(code)) {
            throw new MyException("请求状态已经改变为 " + code);
        }
        apply.setStatus(code);
        baseMapper.updateById(apply);
        return apply;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ActivityToUser> queryActivityToUsersByReceiveUserId(String userId) {

        return this.baseMapper.queryActivityToUsersByReceiveUserId(userId);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ActivityToUser> queryActivityToUsersBySendUserId(String userId) {
        ActivityToUser query = new ActivityToUser();
        query.setUserId(userId);
        return list(new QueryWrapper<>(query));
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ActivityToUser> queryActivityToUsersByUserId(String userId, Integer status) {
        ActivityToUser activityToUser = new ActivityToUser();
        activityToUser.setUserId(userId);
        activityToUser.setStatus(status);
        return this.list(new QueryWrapper<>(activityToUser));
    }
}
